﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvViewer.MpTvService;

namespace TvViewer
{
  public partial class SchedulesForm : Form
  {
    ServiceInterface m_server;
    public SchedulesForm(ServiceInterface _server)
    {
      InitializeComponent();
      m_server = _server;
      WebSchedule[] schedules = m_server.GetAllSchedules();
      FillScheduleListView(schedules);
    }

    private void FillScheduleListView(WebSchedule[] _schedules)
    {
      lvSchedules.Items.Clear();
      foreach (WebSchedule s in _schedules)
      {
        ListViewItem item = new ListViewItem(s.startTime.ToString());
        item.SubItems.Add(s.endTime.ToString());
        item.SubItems.Add(s.channelName);
        item.SubItems.Add(s.programName);
        item.SubItems.Add(s.scheduleTypeStr);
        item.Tag = s;
        lvSchedules.Items.Add(item);
      }
    }

    private void cmdRefreshSchedules_Click(object sender, EventArgs e)
    {
      WebSchedule[] schedules = m_server.GetAllSchedules();
      FillScheduleListView(schedules);
    }

    private void deleteRecordingToolStripMenuItem_Click(object sender, EventArgs e)
    {
      if (lvSchedules.SelectedItems != null && lvSchedules.SelectedItems.Count == 1)
      {
        WebSchedule schedule = lvSchedules.SelectedItems[0].Tag as WebSchedule;
        if (m_server.DeleteSchedule(schedule.idSchedule))
        {
          lvSchedules.Items.Remove(lvSchedules.SelectedItems[0]);
        }
      }
    }

    private void cmdAddNewRecording_Click(object sender, EventArgs e)
    {
      RecordingManualInputBox frmManualRec = new RecordingManualInputBox(m_server);
      if (frmManualRec.ShowDialog(this) == DialogResult.OK)
      {//add recording
        if (m_server.AddSchedule(frmManualRec.SelectedChannel.idChannel, frmManualRec.RecordingName,
                             frmManualRec.BeginTime, frmManualRec.EndTime, 0))
        {
          WebSchedule[] schedules = m_server.GetAllSchedules();
          FillScheduleListView(schedules);
        }
        else
        {
          MessageBox.Show("Error adding schedule");
        }
      }
    }
  }
}
